﻿using MKAdminCore.DTO.CusAttribute;
using MKAdminCore.DTO.Web.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Text;

namespace MKAdminCore.DTO.Web.CusAttribute
{
    /// <summary>
    /// 比较验证
    /// </summary>
    public class CusCompareAttribute : AbstractCusAttribute
    {
        /// <summary>
        /// 需要对比的字段名
        /// </summary>
        public string CompareName { get; set; }

        /// <summary>
        /// 验证是否为空
        /// </summary>
        /// <param name="oValue"></param>
        /// <returns></returns>
        public override HttpResponseResult Validate<T>(T dto, PropertyInfo prop)
        {
            var result = new HttpResponseResult();
            //获取字段内容
            object oValue = prop.GetValue(dto);

            //对比字段内容
            Type type = dto.GetType();
            var property = type.GetProperty(this.CompareName);
            object compareValue = property.GetValue(dto);

            result.Code = (oValue == compareValue) ? 200 : 400;
            if (result.Code != 200)
            {
                result.Msg = base.ErrorMsg;
            }

            return result;
        }
    }
}
